package com.softtown.modules.sys.controller;

import com.softtown.common.annotation.LogOperation;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.utils.Result;
import com.softtown.modules.sys.dto.SysFeedBackReplyDTO;
import com.softtown.modules.sys.service.SysFeedBackReplyService;
import com.softtown.modules.sys.vo.SysFeedBackReplyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @version v1.0
 * @ProjectName: softtown-aioffice
 * @ClassName: SysFeedBackReplyController
 * @Description: 用户反馈回复Controller层
 * @Author: fuming
 * @Date: 2019/7/1 16:17
 */
@RestController
@RequestMapping("/user/feedback")
@Api(tags = "用户反馈回复")
public class SysFeedBackReplyController {

    @Resource
    private SysFeedBackReplyService sysFeedBackReplyService;

    /**
     * @param @param [sysFeedBackReplyDTO]
     * @return @return com.softtown.common.utils.Result
     * @throws
     * @Title: addFeedbackReply
     * @Description: 添加反馈回复
     * @author fuming
     * @date 2019/7/1 21:32
     */
    @PostMapping("feedbackReply")
    @ApiOperation("反馈回复")
    @LogOperation("反馈回复")
    public Result addFeedbackReply(@RequestBody SysFeedBackReplyDTO sysFeedBackReplyDTO) {
        Result result = new Result<>();
        if ("".equals(sysFeedBackReplyDTO.getContent()) && sysFeedBackReplyDTO.getStatus() == 0) {
            return result.error(ErrorCode.REPLY_CONTENT_EMPTY_ERROR);
        }
        sysFeedBackReplyService.addFeedbackReply(sysFeedBackReplyDTO);
        return result;
    }

    /**
     * 获取用户和客服反馈回复
     *
     * @param id 反馈id
     * @return com.softtown.common.utils.Result<java.util.List < com.softtown.modules.sys.vo.SysFeedBackReplyVO>>
     * @author fuming
     * @date 2019/12/2 10:49
     */
    @PostMapping("getFbReplies")
    @ApiOperation("获取用户和客服反馈回复")
    @LogOperation("获取用户和客服反馈回复")
    public Result<List<SysFeedBackReplyVO>> getFbReplies(@RequestBody String id) {
        String fbId = JSONObject.fromObject(id).get("fbId").toString();
        List<SysFeedBackReplyVO> feedbackReplyList = sysFeedBackReplyService.selectFbReplies(Long.valueOf(fbId));
        return new Result<List<SysFeedBackReplyVO>>().ok(feedbackReplyList);
    }

    /**
     * 获取客服反馈回复id
     *
     * @param id 反馈id
     * @return com.softtown.common.utils.Result<java.util.List < java.lang.Long>>
     * @Author fuming
     * @Date 2019/12/6 10:14
     */
    @PostMapping("getFbRplyList")
    @ApiOperation("获取客服反馈回复id")
    @LogOperation("获取客服反馈回复id")
    public Result<List<Long>> getFbRplyList(@RequestBody String id) {
        String fbId = JSONObject.fromObject(id).get("fbId").toString();
        List<Long> oprFbRplyList = sysFeedBackReplyService.selectOprFbRply(Long.valueOf(fbId));
        return new Result<List<Long>>().ok(oprFbRplyList);
    }

    /**
     * 更新客服反馈回复的已读状态
     *
     * @param sysFeedBackReplyDTO 反馈回复DTO
     * @return com.softtown.common.utils.Result
     * @Author fuming
     * @Date 2019/12/6 11:16
     */
    @PostMapping("updateOprReadById")
    @ApiOperation("更新客服反馈回复的已读状态")
    @LogOperation("更新客服反馈回复的已读状态")
    public Result updateOprReadById(@RequestBody SysFeedBackReplyDTO sysFeedBackReplyDTO) {
        sysFeedBackReplyService.updateOprRplyReadById(sysFeedBackReplyDTO.getId());
        return new Result<>();
    }

    /**
     * 标识反馈回复表为已读
     *
     * @param feedbackId
     * @return
     */
    @GetMapping("markRead")
    @ApiOperation("反馈已读")
    @LogOperation("反馈已读")
    public Result read(@ApiIgnore @RequestParam Long feedbackId) {
        sysFeedBackReplyService.markRead(feedbackId);
        return new Result();
    }
}
